package com.meihua.code.hash;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @program: leetcode
 * @description:
 * @author: meihua
 * @created: 2021/09/23 13:52
 */
public class ConsistencyHash {

    private LinkedList<Node> list = new LinkedList<>();

    class Node{
        private Integer hash;
        private HashMap<String,String> v;
        public Node(Integer hash, HashMap<String, String> v) {
            this.hash = hash;
            this.v = v;
        }
    }


    public ConsistencyHash(int initialCapacity) {
        if (initialCapacity<1){
            throw  new RuntimeException("initialCapacity < 1 ! ");
        }
        for (int i=1;i<=initialCapacity;i++){
            Integer key = (int ) (Integer.MAX_VALUE * ( i*1.0 / initialCapacity ));
            list.add(new Node(key,new HashMap<>(1024) ));
        }
    }

    public void put(String key , String value){
        getNode(key).put(key,value);
    }

    public String get(String key){
      return   getNode(key).get(key);
    }

    public HashMap<String, String> getNode(String key){
        for (Node  node: list){
            int hashCode = getHashCode(key);
            System.out.println("---------"+key+" "+hashCode);
            if (hashCode<=node.hash){
                return node.v;
            }
        }
        throw new  RuntimeException(" node ");
    }

    public int getHashCode(String key){
       return Math.abs(key.hashCode()%Integer.MAX_VALUE) ;
    }

    public void addNode(Integer index){
        Iterator<Node> it = list.iterator();
        int i=0;
        while (it.hasNext()) {
            Node node =it.next();
            if (index<node.hash){
                Node newNode = new Node(index,new HashMap<>(1024));
                list.add(i,newNode);
                Iterator<Entry<String, String>> iterator = node.v.entrySet().iterator();
                while (iterator.hasNext()){
                    Entry<String, String> next = iterator.next();
                    if (getHashCode(next.getKey())<=index){
                        newNode.v.put(next.getKey(),next.getValue());
                        node.v.remove(next.getKey());
                    }
                }
                break;
            }
            i++;
        }

        //需要处理 添加为末尾节点 去首节点取数据的场景

    }

    public void removeNode(int index){

        Iterator<Node> iterator = list.iterator();
        while (iterator.hasNext()) {
            Node node = iterator.next();
            if (node.hash==index){
                //如果删除末尾 则漂移到头节点
                Node migrationNode=!iterator.hasNext()?list.getFirst():iterator.next();
                node.v.forEach((k, v)->{
                    migrationNode.v.put(k,v);
                });
                list.remove(node);
                break;
            }
        }
    }



    public static void main(String[] args) {
        ConsistencyHash consistencyHash = new ConsistencyHash(3);
        consistencyHash.put("aaaaa","aaaaa");
        consistencyHash.put("bbbbb","bbbbb");
        consistencyHash.put("cccccccc","cccccccc");
        consistencyHash.put("dddddd","dddddd");
        consistencyHash.put("fff","fff");

        System.out.println(consistencyHash.get("aaaaa"));
        System.out.println(consistencyHash.get("bbbbb"));
        System.out.println(consistencyHash.get("cccccccc"));
        System.out.println(consistencyHash.get("dddddd"));
        System.out.println(consistencyHash.get("fff"));


        consistencyHash.addNode(101286);

        consistencyHash.removeNode(101286);

        System.out.println(consistencyHash.get("fff"));
    }



}
